/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chatsystem;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import protocol.Signal;

/**
 *
 * @author remires
 */
public class NI extends Thread{

    private ChatSystemControler csc;
    int portEcoute;
    boolean suspendu = false;

    public NI(ChatSystemControler csc, int port) {
        this.csc = csc;
        this.portEcoute=port;
    }

    @Override
    public void run() {
           try {
                synchronized(this){
                        while(suspendu)
                                wait();
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(NI.class.getName()).log(Level.SEVERE, null, ex);
            }

        try {
            receiveSignal(portEcoute);
        } catch (SocketException ex) {
            Logger.getLogger(NI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(NI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void receiveSignal(int sport) throws SocketException, IOException {
        int length = 1024;
        byte[] buf = new byte[length];

        DatagramSocket socket = new DatagramSocket(sport);
        DatagramPacket packetToReceive = new DatagramPacket(buf, length);

        while (true) {
            System.out.println("J'attends de recevoir un message sur le port "+sport);
            socket.receive(packetToReceive);

            csc.processSignal(packetToReceive);
        }
    }

    public void sendSignalBroadcast(Signal s) throws SocketException, UnknownHostException, IOException {
        int length = 1024;
        byte[] buf = new byte[length];

        buf = s.serializer();

        /* Création du socket destiné à l'envoi de paquets */
        DatagramSocket socket = new DatagramSocket();

        /* Création du paquet à envoyer */
        //DatagramPacket packetToSend = new DatagramPacket(buf, buf.length, InetAddressUtil.getLocalBroadcast(), 1234);
        DatagramPacket packetToSend = new DatagramPacket(buf, buf.length, InetAddress.getByName("255.255.255.255"), 1234);

        /* Envoi du paquet */
        socket.send(packetToSend);
    }

    public void sendSignal(Signal s, User dest) throws SocketException, IOException {
        int length = 1024;
        byte[] buf = new byte[length];

        buf = s.serializer();

        /* Création du socket destiné à l'envoi de paquets */
        DatagramSocket socket = new DatagramSocket();

        /* Création du paquet à envoyer */
        DatagramPacket packetToSend = new DatagramPacket(buf, buf.length, dest.getAddress(), 1234);

        /* Envoi du paquet */
        socket.send(packetToSend);
    }

    public synchronized void pause(){
            suspendu = true;
    }

    public synchronized void restart(){
            suspendu = false;
            notify();
    }

    public boolean isSuspendu(){
            return suspendu;
    }
}
